<?php
class helper_disposeparam
{
	/**
	 * 
	 * Enter description here ...
	 * @var string安全的搜索连接符
	 */
	static $safe_method = array('like','=','>','<','in');
	/**
	 * 
	 * Enter description here ...
	 * @var string安全的搜索方向
	 */
	static $safe_type = array('or','and');
	
	static $default_type = 'and';
	static $default_method = '=';
	
	private $sql_key ='';
	private $sql_value = array();
	
	/**
	 * 
	 * Enter description here ...
	 * @var array()
	 */
	private $param ;
	/**
	 * 
	 *2011-12-22-上午10:58:16 by 460932465
	 * 初始化
	 * @param $_POST|$_GET $array
	 */
	function __construct($array)
	{
		$this->param = $array;
	}
	function getsql()
	{
		$this->sql_key = ' 1=1 ';
		$this->sql_value = array();
		$_querymethod = isset($this->param['_querymethod'])?$this->param['_querymethod']:array();
		$_querytype = isset($this->param['_querytype'])?$this->param['_querytype']:array();
		//清除附加
		if(count($_querymethod)>0)unset($this->param['_querymethod']);
		if(count($_querymethod)>0)unset($this->param['_querytype']);
		
		foreach ($this->param as $k=>$v)
		{
			$type = isset($_querytype[$k])?$this->_format_type($_querytype[$k]):self::$default_type;
			$method = isset($_querymethod[$k])?$this->_format_method($_querymethod[$k]):self::$default_method;	
			//如果值是array
			if (is_array($v))
			{
			    $array = array();
			    foreach ($v as $_v)
			    {
			        $array[] = $k .' = ' . $_v;
			    }
			    $this->sql_key .= $type . ' ( ' . implode(' OR ', $array).' )';
					//$this->sql_key .= $type." {$k} in (".implode(",",$v).") ";
					//$this->sql_value[] = implode("','",$v);
			}else if (strlen($v) == 0 )
			{
					continue;
			}else if (substr($k, 0,1) == '_')
			{
				continue;
			}
			else
			{
				
				
				if($method == 'like')
				{
					
					$v = "%{$v}%";
				}
				if(strpos($k,'_begin'))
				{
					$method = ">=";
					$k = str_replace('_begin', '', $k);
					$v = ($v);
				}else if(strpos($k,'_end'))
				{
					$method = "<";
					$k = str_replace('_end', '', $k);
					$v = ($v);
				}else if (strpos($k, '_timestart'))
				{
					$method = ">=";
					$k = str_replace('_timestart', '', $k);
					$v = strtotime($v);
				}else if(strpos( $k,'_timeend'))
				{
					$method = "<";
					$k = str_replace('_timeend', '', $k);
					$v = strtotime($v);
				}
					$this->sql_key .= $type." {$k} {$method} ? ";
					$this->sql_value[] = $v;					
			}
			
		}
		array_unshift($this->sql_value,$this->sql_key);
		return $this->sql_value;
	}
	protected function _format_type($type)
	{
		$type = strtolower($type);
		return in_array($type, self::$safe_type)?$type:self::$default_type;
	}
	protected function _format_method($method)
	{
		$method = strtolower($method);
		return in_array($method, self::$safe_method)?$method:self::$default_method;
	}
	public static function parse($array)
	{
		if(is_array($array) && count($array)>1)
		{
			$str = $array[0];
			
			unset($array[0]);
			
			foreach ($array as $k=>$v)
			{
				
				//$str = str_replace('?', $v, $str,1);
				
			}
			return $str;
		}
		return '';
		
	}
}

?>